package com.d3works.apps.account.web;

import com.d3works.apps.account.db.ReconcileGroup;
import com.d3works.apps.account.db.repo.ReconcileGroupRepository;
import com.d3works.apps.account.service.ReconcileGroupService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.ResponseMessageType;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;

public class ReconcileGroupController {

  public void letter(ActionRequest request, ActionResponse response) {

    ReconcileGroupRepository reconcileGroupRepo = Beans.get(ReconcileGroupRepository.class);
    ReconcileGroup reconcileGroup =
        reconcileGroupRepo.find(request.getContext().asType(ReconcileGroup.class).getId());

    if (reconcileGroup != null) {
      try {
        ReconcileGroupService reconcileGroupService = Beans.get(ReconcileGroupService.class);
        reconcileGroupService.letter(reconcileGroup);
        reconcileGroup = reconcileGroupRepo.find(reconcileGroup.getId());
        reconcileGroupService.updateStatus(reconcileGroup);
      } catch (D3worksException e) {
        TraceBackService.trace(response, e, ResponseMessageType.ERROR);
      }
    }
    response.setReload(true);
  }

  public void unletter(ActionRequest request, ActionResponse response) {

    ReconcileGroup reconcileGroup =
        Beans.get(ReconcileGroupRepository.class)
            .find(request.getContext().asType(ReconcileGroup.class).getId());

    if (reconcileGroup != null) {
      try {
        Beans.get(ReconcileGroupService.class).unletter(reconcileGroup);
      } catch (D3worksException e) {
        TraceBackService.trace(response, e, ResponseMessageType.ERROR);
      }
    }
    response.setReload(true);
  }
}
